import APPConfig from '../resource/file/appConfig/JJAPPConfig.js';
import APPURLList from '../resource/file/urlConfig/JJUrlConfig.js';
import { JJLog, JJUtil } from './../util/index'

const { EnvironmentNameValueList, EnvironmentName } = APPConfig;
const { Base, Environment } = APPURLList;

export default class JJHttpHelper {

    static serverHostWithIdentity(identity) {

        if (JJUtil.isNotNull(identity)) {

            let hostKey = "SERVER_HOST_URL";
            let host = '';

            //Evironment
            let hostParams = Environment[EnvironmentName][identity];
            if (!JJUtil.isNull(hostParams)) {
                host = hostParams[hostKey];
                if (JJUtil.isNotNull(host)) {
                    return host;
                }
            }

            //Base
            hostParams = Base[identity];
            if (!JJUtil.isNull(hostParams)) {
                host = hostParams[hostKey];
                if (JJUtil.isNotNull(host)) {
                    return host;
                }
            }

            //Evironment(base)
            host = Environment[EnvironmentName][hostKey];
            if (JJUtil.isNotNull(host)) {
                return host;
            } else {
                JJLog.log("JJLog: identity :" + identity + "don't have SERVER_HOST_URL");
                return '';
            }

        } else {
            JJLog.log("JJLog: identity can't be null");
            return '';
        }
    }

    static serverPathWithIdentity(identity) {

        if (JJUtil.isNotNull(identity)) {
            let pathKey = "SERVER_PATH_URL";
            let path = '';

            //Evironment
            let pathParams = Environment[EnvironmentName][identity];
            if (!JJUtil.isNull(pathParams)) {
                path = pathParams[pathKey];
                if (JJUtil.isNotNull(path)) {
                    return path;
                }
            }

            //Base
            pathParams = Base[identity];
            if (!JJUtil.isNull(pathParams)) {
                path = pathParams[pathKey];
                if (JJUtil.isNotNull(path)) {
                    return path;
                } else {
                    JJLog.log("JJLog: identity :" + identity + "don't have SERVER_PATH_URL");
                    return '';
                }
            } else {
                JJLog.log("JJLog: identity :" + identity + "don't have SERVER_PATH_URL");
                return '';
            }
        } else {
            JJLog.log("JJLog: identity can't be null");
            return '';
        }
    }

    static getValue(identity, key) {
        if (JJUtil.isNotNull(identity)) {
            let path = '';

            //Evironment
            let pathParams = Environment[EnvironmentName][identity];
            if (!JJUtil.isNull(pathParams)) {
                path = pathParams[key];
                if (JJUtil.isNotNull(path)) {
                    return path;
                }
            }

            //Base
            pathParams = Base[identity];
            if (!JJUtil.isNull(pathParams)) {
                path = pathParams[key];
                if (JJUtil.isNotNull(path)) {
                    return path;
                } else {
                    JJLog.log("JJLog: identity :" + identity + "don't have SERVER_PATH_URL");
                    return '';
                }
            } else {
                JJLog.log("JJLog: identity :" + identity + "don't have SERVER_PATH_URL");
                return '';
            }
        } else {
            JJLog.log("JJLog: identity can't be null");
            return '';
        }
    }
}
